<!--手机商城网站的顶部导航栏，，包含 logo、导航菜单和用户信息，-->
<template>
  <header class="header">
    <div class="header-container">
      <div class="logo">
        <router-link to="/" class="logo-link">  <!--品牌 Logo-->>
          📱 手机商城
        </router-link>
      </div>
      <!--导航菜单
      首页 / 用户中心链接：基础导航入口，支持 Vue Router 跳转
     条件渲染：仅当用户已登录时显示 "设置" 链接
      购物车链接：包含🛒图标和购物车徽章，实时显示商品数量-->>
      <nav class="nav">
        <router-link to="/" class="nav-link">首页</router-link>
        <router-link to="/user" class="nav-link">用户中心</router-link>
        <router-link v-if="userStore.isLoggedIn" to="/settings" class="nav-link">设置</router-link>
        <router-link to="/cart" class="nav-link cart-link">
          🛒 购物车
          <span v-if="cartStore.totalItems > 0" class="cart-badge">
            {{ cartStore.totalItems }}
          </span>
        </router-link>
      </nav>
      <!--用户信息区
登录状态判断：
已登录：显示 "欢迎，用户名"
未登录：显示 "登录" 按钮链接到用户中心
通过v-if和v-else实现不同状态的界面切换
响应式数据：
userStore.isLoggedIn：判断用户是否登录
userStore.userInfo.name：获取用户名
cartStore.totalItems：获取购物车商品数量-->>
      <div class="user-info">
        <span v-if="userStore.isLoggedIn" class="welcome-text">
          欢迎，{{ userStore.userInfo.name }}
        </span>
        <span v-else class="login-prompt">
          <router-link to="/user" class="login-link">登录</router-link>
        </span>
      </div>
    </div>
  </header>
</template>
<!--引入状态管理：
import { useUserStore } from '../stores/user'：获取用户登录状态和信息
import { useCartStore } from '../stores/cart'：获取购物车商品总数-->>
<script setup>
import { useUserStore } from '../stores/user'
import { useCartStore } from '../stores/cart'

const userStore = useUserStore()
const cartStore = useCartStore()
</script>

<style scoped>
.header {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70px;
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
}

.logo-link {
  color: #007acc;
  text-decoration: none;
  transition: color 0.2s ease;
}

.logo-link:hover {
  color: #005c99;
}

.nav {
  display: flex;
  align-items: center;
  gap: 32px;
}

.nav-link {
  color: #333;
  text-decoration: none;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 6px;
  transition: all 0.2s ease;
  position: relative;
}

.nav-link:hover {
  background-color: #f8f9fa;
  color: #007acc;
}

.nav-link.router-link-active {
  color: #007acc;
  background-color: #e3f2fd;
}

.cart-link {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cart-badge {
  background-color: #e74c3c;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

.user-info {
  font-size: 0.9rem;
}

.welcome-text {
  color: #666;
  font-weight: 500;
}

.login-link {
  color: #007acc;
  text-decoration: none;
  font-weight: 600;
  padding: 8px 16px;
  border: 2px solid #007acc;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.login-link:hover {
  background-color: #007acc;
  color: white;
}

@media (max-width: 768px) {
  .header-container {
    padding: 0 16px;
    height: 60px;
  }
  
  .logo {
    font-size: 1.2rem;
  }
  
  .nav {
    gap: 16px;
  }
  
  .nav-link {
    padding: 6px 12px;
    font-size: 0.9rem;
  }
  
  .user-info {
    font-size: 0.8rem;
  }
  
  .welcome-text {
    display: none;
  }
}

@media (max-width: 480px) {
  .nav {
    gap: 8px;
  }
  
  .nav-link {
    padding: 4px 8px;
    font-size: 0.8rem;
  }
  
  .cart-link {
    gap: 4px;
  }
}
</style> 